package com.hmdp;

import cn.hutool.json.JSONUtil;
import com.hmdp.dto.LoginFormDTO;
import com.hmdp.dto.Result;
import com.hmdp.entity.User;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisWorker;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootTest
@Slf4j
@AutoConfigureMockMvc
class HmDianPingApplicationTests {

    @Resource
    private RedisWorker redisWorker;

    @Resource
    private IUserService userService;

    private ExecutorService executorService = Executors.newFixedThreadPool(500);

    @Autowired
    private MockMvc mockMvc;

    @Test
    void UserTokenBuildTest() throws Exception {

        String phone = null;
        String code = null;

        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("D:\\CodeTools\\apache-jmeter-5.6.3\\dyxTest\\token.txt"));

        for(int i = 1; i<= 1010; i++ ){
            User user = userService.getById(i);
            if(user == null){
                continue;
            }
            phone = user.getPhone();
            log.info("phone: {}", phone);
            if (phone == null || phone.isEmpty()) {
                log.warn("User with id {} has no phone number", i);
                continue;
            }
            // 模拟获取验证码
            ResultActions perform = mockMvc.perform(
                    MockMvcRequestBuilders.post("/user/code?phone=" + phone));
            // 获取响应结果
            String contentAsString = perform.andReturn().getResponse().getContentAsString();
            // 将结果转化为Result对象
            Result result = JSONUtil.toBean(contentAsString, Result.class);
            // 获取code
            code = result.getData().toString();
            LoginFormDTO loginFormDTO = new LoginFormDTO();
            loginFormDTO.setPhone(phone);
            loginFormDTO.setCode(code);
            // 模拟登录
            ResultActions perform2 = mockMvc.perform(
                    MockMvcRequestBuilders.post("/user/login")
                            // 设置contentType表示为json信息
                            .contentType("application/json")
                            // 设置content
                            .content(JSONUtil.toJsonStr(loginFormDTO)));
            String contentAsString1 = perform2.andReturn().getResponse().getContentAsString();
            Result result2 = JSONUtil.toBean(contentAsString1, Result.class);
            // 获取token
            String token = (String) result2.getData();
            log.info("token: {}", token);
            // 写入文件
            writer.write(token + "\n");

        }

    }


    @Test
    void RedisWorkerTest() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(300);

        Runnable task = () -> {
            for (int i = 0; i < 100; i++) {
                long orderId = redisWorker.nextId("order");
                log.info("orderId: {}", orderId);
            }
            latch.countDown();
        };

        long begin = System.currentTimeMillis();
        for (int i = 0; i < 300; i++) {
            executorService.submit(task);
        }
        latch.await();
        long end = System.currentTimeMillis();
        System.out.println("time: " + (end - begin));


    }


}
